package model;

import app.App;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Product {

    private int productId;
    private String productName;
    private String category;
    private float sellingPrice;
    private float purchasePrice;
    private int quantity;

    public Product() {
    }

    public Product(int productId, String productName, String category,  float sellingPrice, float purchasePrice,int quantity) {
        this.productId = productId;
        this.productName = productName;
        this.category = category;
        this.sellingPrice = sellingPrice;
        this.purchasePrice = purchasePrice;
        this.quantity = quantity;
    }

    public Product(int productId, String productName, String category, float sellingPrice) {
        this.productId = productId;
        this.productName = productName;
        this.category = category;
        this.sellingPrice = sellingPrice;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public float getSellingPrice() {
        return sellingPrice;
    }

    public void setSellingPrice(float sellingPrice) {
        this.sellingPrice = sellingPrice;
    }

    public float getPurchasePrice() {
        return purchasePrice;
    }

    public void setPurchasePrice(float purchasePrice) {
        this.purchasePrice = purchasePrice;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Object[] toProductListObjectArray() {
        return new Object[]{productId, productName, category, sellingPrice, purchasePrice, quantity};
    }

    public Object[] toSaleListObjectArray() {
        return new Object[]{productId, productName, sellingPrice, quantity, String.format("%.2f", sellingPrice*quantity)};
    }

    @Override
    public String toString() {
        return "Product{" +
                "productId=" + productId +
                ", productName='" + productName + '\'' +
                ", category='" + category + '\'' +
                ", sellingPrice=" + sellingPrice +
                ", purchasePrice=" + purchasePrice +
                ", quantity=" + quantity +
                '}';
    }

    //根据商品Id获取商品
    public static Product getProduct(int productId) {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM product WHERE `productid` = ?");
            ps.setObject(1, productId);
            ResultSet rs = ps.executeQuery();
            Product product = new Product();
            if (rs.next()) {
                product.setProductId(rs.getInt("productid"));
                product.setProductName(rs.getString("productname"));
                product.setCategory(rs.getString("category"));
                product.setSellingPrice(rs.getFloat("sellingprice"));
                product.setPurchasePrice(rs.getFloat("purchaseprice"));
                product.setQuantity(rs.getInt("quantity"));
                conn.close();
                return product;
            }
            else {
                conn.close();
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    //获取商品列表
    public static List<Product> getProducts() {
        List<Product> productList = new ArrayList<>();

        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM product");
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                Product product = new Product();
                product.setProductId(rs.getInt("productid"));
                product.setProductName(rs.getString("productname"));
                product.setCategory(rs.getString("category"));
                product.setSellingPrice(rs.getFloat("sellingprice"));
                product.setPurchasePrice(rs.getFloat("purchaseprice"));
                product.setQuantity(rs.getInt("quantity"));
                System.out.println(product);
                productList.add(product);
            }
            conn.close();
            return productList;
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }



    //添加商品，修改该商品的Id对应的商品信息
    public static boolean modProduct(Product newProductInfo) {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement(
                    "UPDATE `product` SET `productname` = ?, `category` = ?, `sellingprice` = ?  WHERE (`productid` = ?);");
            ps.setObject(1, newProductInfo.getProductName());
            ps.setObject(2, newProductInfo.getCategory());
            ps.setObject(3, newProductInfo.getSellingPrice());
            ps.setObject(4, newProductInfo.getProductId());
            int result = ps.executeUpdate();
            conn.close();//关闭连接
            if (result == 1) {
                System.out.println("修改成功！");
                return true;
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        System.out.println("修改失败！");
        return false;
    }

    //获取一个新的商品号
    public static int getNewProductId() {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT MAX(productid) AS max FROM product");
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int max = rs.getInt("max") + 1;
                conn.close();
                return max;
            }
            conn.close();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return 1;
    }
}
